# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
import cv2
import numpy as np
#from matplotlib import pyplot as plt
import math
import time

img = cv2.imread('F:/awei/code/tmp/4.jpg',0)
img2 = img.copy()
template = cv2.imread('F:/awei/code/tmp/tmp1.jpg',0)
w, h = template.shape[::-1]

# All the 6 methods for comparison in a list
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
            'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']

img = img2.copy()
meth = methods[1]
method = eval(meth)

# Apply template Matching
res = cv2.matchTemplate(img,template,method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
    top_left = min_loc
else:
    top_left = max_loc

cv2.drawMarker( img, top_left, (0,0,255))
cv2.namedWindow("img", cv2.WINDOW_NORMAL)
cv2.imshow("img",img)
        
#    print( 1, meth)    
#point1.append(top_left)

##point1 = list()
##point2 = list()
##start = time.clock()
##t = cv2.getTickCount();
##for meth in methods:
##    img = img2.copy()
##    method = eval(meth)
##
##    # Apply template Matching
##    res = cv2.matchTemplate(img,template,method)
##    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
##
##    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
##    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
##        top_left = min_loc
##    else:
##        top_left = max_loc
##        
###    print( 1, meth)    
##    point1.append(top_left)

##template = cv2.imread('F:/awei/code/tmp/tmp2.jpg',0)
##w, h = template.shape[::-1]   
## 
##for meth in methods:
##    img = img2.copy()
##    method = eval(meth)
##
##    # Apply template Matching
##    res = cv2.matchTemplate(img,template,method)
##    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
##
##    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
##    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
##        top_left = min_loc
##    else:
##        top_left = max_loc
##        
#    print( 2, meth)
##    point2.append(top_left)
##t2 = cv2.getTickCount();
##end = time.clock()
##print("run time:",end-start,"; t:",t,";t2:",t2, "; t2-t:", (t2-t)/cv2.getTickFrequency())
##
##print( point1)
##print( point2)
##
##if( len(point1) == 6 and len(point1) == 6 ):
##    print("开始比较距离")
##    
##    for ind in(range(0,len(point1))):
##        dis = math.sqrt( math.pow(point1[ind][0] - point2[ind][0], 2) + math.pow(point1[ind][1]-point2[ind][1], 2) )
##        print( str(dis),": ",ind )
    
#    bottom_right = (top_left[0] + w, top_left[1] + h)
#
#    cv2.rectangle(img,top_left, bottom_right, 255, 2) 
#    
#    plt.subplot(121),plt.imshow(res,cmap = 'gray')
#    plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
#    plt.subplot(122),plt.imshow(img,cmap = 'gray')
#    plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
#    plt.suptitle(meth)
#
#    plt.show()


#import cv2
#import numpy as np
#from matplotlib import pyplot as plt
#
#img_rgb = cv2.imread('/home/awei/toCode/tmp/1.jpg')
#img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
#template = cv2.imread('/home/awei/toCode/tmp/tmp1.jpg',0)
#w, h = template.shape[::-1]
#
#res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
#threshold = 0.8
#loc = np.where( res >= threshold)
#for pt in zip(*loc[::-1]):
#    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
#
#cv2.imwrite('res.png',img_rgb)

#import cv2 as cv
#
##load image
#filename = "/home/awei/toCode/tmp/1.jpg"
#image = cv.imread(filename)
#
#
##create one window
#win_name = "test"
#cv.namedWindow(win_name)
#win2_name = "test2"
#cv.namedWindow(win2_name)
#
#
##take off one template
##rect = (170,80,50,50)
##cv.SetImageROI(image, rect)
##template = cv.CloneImage(image)
#template = "/home/awei/toCode/tmp/tmp1.jpg"
#cv.imshow(win_name, template)
#
#cv.ResetImageROI(image)
#W,H=cv.GetSize(image)
#w,h=cv.GetSize(template)
#width=W-w+1
#height=H-h+1
#result=cv.CreateImage((width,height),32,1)
#
###result 是一个矩阵，存储了模板与源图像每一帧相比较后的相似值，
#cv.MatchTemplate(image,template, result,cv.CV_TM_SQDIFF)
#
###下面的操作将从矩阵中找到相似值最小的点，从而定位出模板位置
#(min_x,max_y,minloc,maxloc)=cv.MinMaxLoc(result)
#(x,y)=minloc
#cv.Rectangle(image,(int(x),int(y)),(int(x)+w,int(y)+h),(255,255,255),1,0)
#cv.ShowImage(win2_name, image)
#
#cv.WaitKey()
